package com.hjimi.ski.view.activity;

import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.hjimi.ski.R;
import com.hjimi.ski.iView.BaseIView;
import com.hjimi.ski.utils.CustomDialog;
import com.hjimi.ski.utils.Mutils;
import com.hjimi.ski.utils.update.AllDialogShowStrategy;
import com.hjimi.ski.utils.update.NotificationDownloadCreatorMandatory;
import com.hjimi.ski.utils.update.NotificationUpdateCreatorMandatory;
import com.hjimi.ski.utils.update.OkhttpCheckWorker;
import com.hjimi.ski.utils.update.OkhttpDownloadWorker;

import org.lzh.framework.updatepluginlib.UpdateBuilder;

/**
 * Created By 孙岩
 * on 2023/2/1
 */
public class BaseActivity extends AppCompatActivity implements BaseIView {
    //进度条
    protected CustomDialog customDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        customDialog = new CustomDialog(this, R.style.CustomDialog);
        String mCurVersionName = Mutils.getLocalVersionName(this);
        int mCurVersionCode = Mutils.getLocalVersion(this);
        UpdateBuilder builder = UpdateBuilder.create();

        // 根据各项是否选择使用默认配置进行使用更新。
        builder.setCheckWorker(OkhttpCheckWorker.class);
        builder.setUpdateStrategy(new AllDialogShowStrategy());
        builder.setDownloadNotifier(new NotificationDownloadCreatorMandatory());
        builder.setCheckNotifier(new NotificationUpdateCreatorMandatory(builder, mCurVersionName, mCurVersionCode, new NotificationUpdateCreatorMandatory.NotificationUpdateCreatorI() {
            @Override
            public void onClick(int button) {
                if (button == 0) {

                }
            }

            @Override
            public void isNotMandatory() {//不是强制升级

            }
        }));
        builder.setDownloadWorker(OkhttpDownloadWorker.class);
        builder.check();
    }

    @Override
    public void showLoading() {
        if (!customDialog.isShowing()) {
            customDialog.show();
        }
    }

    @Override
    public void hideLoading() {
        if (customDialog.isShowing()) {
            customDialog.dismiss();
        }
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showErr() {
        Toast.makeText(this, "网络请求失败！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public Context getContext() {
        return BaseActivity.this;
    }
}

